<?php

/* Prepara os arquivos necessarios para as tabelas Ligadas
 * 
 */

function mount_js_link($db)
{
     $sql = "SELECT pagina,aba,tabela FROM mount_link GROUP BY pagina,aba,tabela";
     $result = mysql_query($sql, $db);
     if($result == 0)
     {
          printf("<h1>Falha: %s</h1>\n", mysql_error($db));
          printf("<h3>%s</h3>\n", $sql);
          exit;
     }
     else
     {
          if(mysql_num_rows($result) > 0)
          {
               while($myrow = mysql_fetch_row($result))
               {
                    continue;
                    if($myrow[0] == 'Funcionario' && $myrow[2] == 'Funcionario_Evento')
                    {
                         create_js_link($db, $myrow[0], $myrow[1], $myrow[2]);
                         create_ajax_link($db, $myrow[0], $myrow[1], $myrow[2]);
                    }
               }
          }
     }
     //exit;
}

function get_var($db,$table,$page,$aba, &$var, &$SQLP1, &$SQLP2, &$UP)
{
     $SQLP1 = "          \$sql = \"INSERT INTO " . $table . " (id" . strtolower($table) .",id" . strtolower($page);
     $SQLP2 = " VALUES('0','\$id" . strtolower($page) . "'";
     $UP =    "          \$sql = \"UPDATE " . $table . " SET ";
     
     $var = "          \$id" . strtolower($page) . " = \$_REQUEST['id" . strtolower($page) . "'];\n";
     $sql = "SELECT campo,tipo,tamanho FROM mount_link WHERE tabela='$table' AND pagina='$page' AND aba='$aba' order by posicao";
     //printf("<p>%s</p>",$sql);
     $result = mysql_query($sql, $db);
     if($result == 0)
     {
          printf("<h1>Falha: %s</h1>\n", mysql_error($db));
          printf("<h3>%s</h3>\n", $sql);
          exit;
     }
     else
     {
          if(mysql_num_rows($result) > 0)
          {
               $n = 0;
               while($myrow = mysql_fetch_row($result))
               {
                    $SQLP1 .= ",$myrow[0]";
                    $SQLP2 .= ",'\$$myrow[0]'";
                    if($n == 0)
                    {
                         $UP .= "$myrow[0]='\$$myrow[0]'";
                    }
                    else
                    {
                         $UP .= ",$myrow[0]='\$$myrow[0]'";
                    }
                    if($myrow[1] == 'date')
                    {
                         $var .= "          $" . strtolower($myrow[0]) . " = mysql_real_escape_string(\$_REQUEST['" . strtolower($myrow[0]) . "']);\n";
                         $var .= "          $" . strtolower($myrow[0]) . " = sprintf(\"%s-%s-%s\", substr($" . strtolower($myrow[0]) . ", 6, 4), substr($" . strtolower($myrow[0]) . ", 3, 2), substr($" . strtolower($myrow[0]) . ", 0, 2));\n";
                    }
                    else
                    {
                         $var .= "          $" . strtolower($myrow[0]) . " = mysql_real_escape_string(\$_REQUEST['" . strtolower($myrow[0]) . "']);\n";
                         if($myrow[0] == 'cpf')
                         {
                                   $var .= "          $" . strtolower($myrow[0]) . " = limpa(\$" . strtolower($myrow[0]) . ");\n";
                         }
                    }
                    $n++;
               }
          }
     }
     $SQLP1 .= ")";
     $SQLP2 .= ")\";\n";
     $UP .= " WHERE id" . strtolower($table) ."='\$id'\";\n";
     return;
}

function get_where($db, $table, $page, $aba, &$WHERE, &$SQL, &$ROW, &$FROM)
{
     $SQL = "\$SQL = \"SELECT id".strtolower($table)." as id";
     $FROM = " FROM $table";
     $sql = "SELECT campo,tipo,tamanho FROM mount_link WHERE tabela='$table' AND pagina='$page' AND aba='$aba' order by posicao";
     //printf("<p>%s</p>",$sql);
     $result = mysql_query($sql, $db);
     if($result == 0)
     {
          printf("<h1>Falha: %s</h1>\n", mysql_error($db));
          printf("<h3>%s</h3>\n", $sql);
          exit;
     }
     else
     {
          if(mysql_num_rows($result) > 0)
          {
               $n = 0;
               while($myrow = mysql_fetch_row($result))
               {
                    if(substr(strtolower($myrow[0]), 0, 2) == "id")
                    {
                         $sel = ucfirst(substr($myrow[0], 2, strlen($myrow[0]) - 2));
                         if(strstr($sel,"_") != '')
                         {
                              $ar = explode("_",$sel);
                              $s = ucfirst(strtolower($ar[0])) . "_" . ucfirst(strtolower($ar[1]));
                              $sel = $s;
                         }
                         $WHERE .= " AND $table.$myrow[0]=$sel.$myrow[0]";
                         $FROM .= ",$sel";
                         if($n > 0)
                         {
                              $SQL .= ",";
                         }
                         $SQL .= "$sel.descricao as $myrow[0]";
                         if($n > 0)
                         {
                              $ROW .= ",";
                         }
                         if($myrow[0] == 'cpf')
                         {
                              $ROW .= "FormataCNPJ(\$row[$myrow[0]])";
                         }
                         else
                         {
                              $ROW .= "\$row[$myrow[0]]";
                         }
                    }
                    else
                    {
                         if($myrow[1] == 'date')
                         {
                              $SQL .= ",DATE_FORMAT($myrow[0],'%d/%m/%Y') as $myrow[0]";
                         }
                         else
                         {
                              $SQL .= ",$myrow[0] as $myrow[0]";
                         }
                         if($n > 0)
                         {
                              $ROW .= ",";
                         }
                         if($myrow[0] == 'cpf')
                         {
                              $ROW .= "FormataCNPJ(\$row[$myrow[0]])";
                         }
                         else
                         {
                              $ROW .= "\$row[$myrow[0]]";
                         }
                    }
                    $n++;
               }
          }
     }
     return;
}

function create_js_link($db, $page, $aba, $table)
{
     $js_lnk = '';
     $js_lnk .= sprintf("\$(document).ready(function() {\n");

     $sql = "SELECT campo FROM mount_link WHERE tabela='$table' AND pagina='$page' AND aba='$aba' order by posicao";
     $result = mysql_query($sql, $db);
     if($result == 0)
     {
          printf("<h1>Falha: %s</h1>\n", mysql_error($db));
          printf("<h3>%s</h3>\n", $sql);
          exit;
     }
     else
     {
          if(mysql_num_rows($result) > 0)
          {
               while($myrow = mysql_fetch_row($result))
               {
                    if(substr(strtolower($myrow[0]), 0, 2) == "id")
                    {
                         $sel = ucfirst(substr($myrow[0], 2, strlen($myrow[0]) - 2));
                         $js_lnk .= "     var " . strtolower($sel) . " = $.ajax({\n";
                         $js_lnk .= "          type: 'GET',\n";
                         $js_lnk .= "          url: '" . strtolower($page) . "_" . strtolower($table) . ".ajax.php',\n";
                         $js_lnk .= "          data: '" . strtolower($sel) . "=1',\n";
                         $js_lnk .= "          async: false, \n";
                         $js_lnk .= "          success: function(data, result) {\n";
                         $js_lnk .= "               if(!result) alert('Falha na Recepção dos Eventos.');\n";
                         $js_lnk .= "          }\n";
                         $js_lnk .= "     }).responseText;\n\n";
                    }
               }
          }
     }
     $js_lnk .= "     jQuery('#list_" . strtolower($page) . "_" . strtolower($table) . "').jqGrid({\n";
     $js_lnk .= "          url:'" . strtolower($page) . "_" . strtolower($table) . ".ajax.php?q=1',\n";
     $js_lnk .= "          datatype: 'json',\n";
     $js_lnk .= "          colNames:[";

     $sql = "SELECT label FROM mount_link WHERE tabela='$table' AND pagina='$page' AND aba='$aba' order by posicao";
     $result = mysql_query($sql, $db);
     if($result == 0)
     {
          printf("<h1>Falha: %s</h1>\n", mysql_error($db));
          printf("<h3>%s</h3>\n", $sql);
          exit;
     }
     else
     {
          if(mysql_num_rows($result) > 0)
          {
               $n = 0;
               while($myrow = mysql_fetch_row($result))
               {
                    if($n == 0)
                    {
                         $js_lnk .= "'" . $myrow[0] . "'";
                    }
                    else
                    {
                         $js_lnk .= ",'" . $myrow[0] . "'";
                    }
                    $n++;
               }
               $js_lnk .= "],\n";
          }
     }
     $js_lnk .= "          colModel:[\n";
     $sql = "SELECT campo,tipo,tamanho FROM mount_link WHERE tabela='$table' AND pagina='$page' AND aba='$aba' order by posicao";
     $result = mysql_query($sql, $db);
     if($result == 0)
     {
          printf("<h1>Falha: %s</h1>\n", mysql_error($db));
          printf("<h3>%s</h3>\n", $sql);
          exit;
     }
     else
     {
          if(mysql_num_rows($result) > 0)
          {
               $n = 0;
               $tamanho = 0;
               while($myrow = mysql_fetch_row($result))
               {
                    $tamanho += $myrow[2];
                    if($n > 0)
                    {
                         $js_lnk .= ",\n";
                    }
                    $js_lnk .= "          {\n";
                    $js_lnk .= "               name:'" . $myrow[0] . "',\n";
                    $js_lnk .= "               index:'" . $myrow[0] . "',\n";
                    if($myrow[1] == 'date')
                    {
                         $js_lnk .= "               align:'center',\n";
                    }
                    else
                    {
                         if(substr(strtolower($myrow[0]), 0, 2) == "id")
                         {
                              $js_lnk .= "               align:'center',\n";
                         }
                         else
                         {
                              if($myrow[0] == 'cpf')
                              {
                                   $js_lnk .= "               align:'right',\n";
                              }
                              else
                              {
                                   $js_lnk .= "               align:'left',\n";
                              }
                         }
                    }
                    $js_lnk .= "               sortable:false,\n";
                    $js_lnk .= "               search:false,\n";
                    $js_lnk .= "               hidden: false,\n";
                    $js_lnk .= "               editable: true,\n";
                    if(substr(strtolower($myrow[0]), 0, 2) == "id")
                    {
                         $sel = ucfirst(substr($myrow[0], 2, strlen($myrow[0]) - 2));
                         $js_lnk .= "               edittype: 'select',\n";
                         $js_lnk .= "               editoptions: {\n";
                         $js_lnk .= "                    value: " . strtolower($sel) . "\n";
                         $js_lnk .= "               }\n";
                    }
                    else
                    {
                         $js_lnk .= "               edittype: 'text',\n";
                         $js_lnk .= "               editoptions: {\n";
                         if($myrow[0] == 'cpf')
                         {
                                   $js_lnk .= "                    size: 14, \n";
                                   $js_lnk .= "                    maxlength: 14,\n";
                                   $js_lnk .= "                    dataInit: function(element) {\n";
                                   $js_lnk .= "                         $(element).mask('999.999.999-99');\n";
                                   $js_lnk .= "                    }\n";
                         }
                         else
                         {
                              if($myrow[1] == 'date')
                              {
                                   $js_lnk .= "                    size: 10,\n";
                                   $js_lnk .= "                    maxlength: 10,\n";
                                   $js_lnk .= "                    dataInit: function(element) {\n";
                                   $js_lnk .= "                         $(element).datepicker({\n";
                                   $js_lnk .= "                              changeMonth: true,\n";
                                   $js_lnk .= "                              changeYear: true\n";
                                   $js_lnk .= "                         });\n";
                                   $js_lnk .= "                         $(element).datepicker('option','yearRange','1900:2020');\n";
                                   $js_lnk .= "                    }\n";
                              }
                              else
                              {
                                   if($myrow[1] == 'varchar')
                                   {
                                        $js_lnk .= "                    size: $myrow[2]\n";
                                   }
                                   else
                                   {
                                        $js_lnk .= "                    size: 40\n";
                                   }
                              }
                         }
                         $js_lnk .= "               }\n";
                    }
                    $js_lnk .= "          }";
                    $n++;
               }
               $js_lnk .= "\n          ],\n";
          }
     }
     $js_lnk .= "          editurl: '" . strtolower($page) . "_" . strtolower($table) . ".ajax.php',\n";
     $js_lnk .= "          altRows: true,\n";
     $js_lnk .= "          autowidth: false,\n";
     $js_lnk .= "          rowNum:10,\n";
     $js_lnk .= "          rowList:[5,10,25,50,100,200,500,1000],\n";
     $js_lnk .= "          pager: '#pager_" . strtolower($page) . "_" . strtolower($table). "',\n";
     $js_lnk .= "          viewrecords: true,\n";
     $js_lnk .= "          scroll: false,\n";
     $js_lnk .= "          scrollrows: false,\n";
     $js_lnk .= "          sortname: 'nome',\n";
     $js_lnk .= "          sortorder: 'asc',\n";
     $js_lnk .= "          width: 800,\n";
     $js_lnk .= "          shrinkToFit: true,\n";
     $js_lnk .= "          height: 260,\n";
     $js_lnk .= "          gridview: true,\n";
     $js_lnk .= "          caption:'$aba',\n";
     $js_lnk .= "          gridComplete: function(){\n";
     $js_lnk .= "          },\n";
     $js_lnk .= "          loadComplete: function () {\n";
     $js_lnk .= "               var url = '" . strtolower($page) . "_" . strtolower($table) . ".ajax.php?id" . strtolower($page) . "=' + current;\n";
     $js_lnk .= "               jQuery('#list_" . strtolower($page) . "_" . strtolower($table) . "').setGridParam({\n";
     $js_lnk .= "                    editurl: url\n";
     $js_lnk .= "               });\n";
     $js_lnk .= "               var cl;\n";
     $js_lnk .= "               var trElement;\n";
     $js_lnk .= "               var rowIDs = jQuery('#list_" . strtolower($page) . "_" . strtolower($table) . "').getDataIDs();\n";
     $js_lnk .= "               for (var i=0;i<rowIDs.length;i=i+1){\n";
     $js_lnk .= "                    cl = rowIDs[i];\n";
     $js_lnk .= "                    trElement = jQuery('\"#'+cl+'\"',jQuery('#list_" . strtolower($page) . "_" . strtolower($table) . "'));\n";
     $js_lnk .= "                    trElement.removeClass('ui-widget-content');\n";
     $js_lnk .= "                    trElement.addClass('rowColorGREEN');\n";
     $js_lnk .= "               }\n";
     $js_lnk .= "          },\n";
     $js_lnk .= "          beforeRequest: function (){\n";
     $js_lnk .= "               var url = '" . strtolower($page) . "_" . strtolower($table) . ".ajax.php?q=1&id" . strtolower($page) . "=' + current;\n";
     $js_lnk .= "               jQuery('#list_" . strtolower($page) . "_" . strtolower($table) . "').setGridParam({\n";
     $js_lnk .= "                    url: url\n";
     $js_lnk .= "              });\n";
     $js_lnk .= "          },\n";
     $js_lnk .= "          onCellSelect: function(idrow,idcell,conteudo){\n";
     $js_lnk .= "          }\n";
     $js_lnk .= "     });\n";
     $js_lnk .= "     jQuery('#list_" . strtolower($page) . "_" . strtolower($table) . "')\n";
     $js_lnk .= "     .navGrid('#pager_" . strtolower($page) . "_" . strtolower($table) . "',{\n";
     $js_lnk .= "          edit:true,\n";
     $js_lnk .= "          add:true,\n";
     $js_lnk .= "          del:true,\n";
     $js_lnk .= "          search:false\n";
     $js_lnk .= "     },{\n";
     $js_lnk .= "          top:300,\n";
     $js_lnk .= "          left:300,\n";
     $js_lnk .= "          width:400\n";
     $js_lnk .= "     },{\n";
     $js_lnk .= "          top:300,\n";
     $js_lnk .= "          left:300,\n";
     $js_lnk .= "          width:400\n";
     $js_lnk .= "     })\n";
     $js_lnk .= "     .gridResize('#pager_" . strtolower($page) . "_" . strtolower($table) . "',{\n";
     $js_lnk .= "          minWidth:350,\n";
     $js_lnk .= "          maxWidth:800,\n";
     $js_lnk .= "          minHeight:80,\n";
     $js_lnk .= "          maxHeight:350\n";
     $js_lnk .= "     })\n";
     $js_lnk .= "     jQuery('#list_" . strtolower($page) . "_" . strtolower($table) . "').trigger('reloadGrid');\n";
     $js_lnk .= "});\n";
     $FILE = "". strtolower($page) . "_" . strtolower($table) . ".js";
     $fPHP = fopen($FILE, "w");
     if(!$fPHP)
     {
          echo "<h1>Falha na Criacao do Arquivo '" . $FILE . "'</h1>";
          exit;
     }
     fprintf($fPHP, "%s", $js_lnk);
     fclose($fPHP);
}

function create_ajax_link($db, $page, $aba, $table)
{
     $ajax_lnk = '';
     $ajax_lnk .= "<?php\n";
     $ajax_lnk .= "error_reporting(0);\n";
     $ajax_lnk .= "session_start();\n";
     $ajax_lnk .= "require 'func.inc.php';\n";
     $ajax_lnk .= "require 'conexao.inc.php';\n";

     $sql = "SELECT campo FROM mount_link WHERE tabela='$table' AND pagina='$page' AND aba='$aba' order by posicao";
     $result = mysql_query($sql, $db);
     if($result == 0)
     {
          printf("<h1>Falha: %s</h1>\n", mysql_error($db));
          printf("<h3>%s</h3>\n", $sql);
          exit;
     }
     else
     {
          if(mysql_num_rows($result) > 0)
          {
               while($myrow = mysql_fetch_row($result))
               {
                    if(substr(strtolower($myrow[0]), 0, 2) == "id")
                    {
                         $sel = ucfirst(substr($myrow[0], 2, strlen($myrow[0]) - 2));
                         if(strstr($sel,"_") != "")
                         {
                              $ar = explode("_",$sel);
                              $tbl = ucfirst(strtolower($ar[0])). "_" . ucfirst(strtolower($ar[1]));
                         }
                         else
                         {
                              $tbl = ucfirst(strtolower($sel));
                         }
                         $ajax_lnk .= "if(isset(\$_REQUEST['" . strtolower($sel) . "']))\n";
                         $ajax_lnk .= "{\n";
                         $ajax_lnk .= "     \$ret = '';\n";
                         $ajax_lnk .= "     \$SQL = \"SELECT id" . strtolower($sel) . ",descricao FROM ".$tbl." ORDER BY descricao\";\n";
                         $ajax_lnk .= "     \$result = mysql_query(\$SQL) or die('Falha na execucao da query' . mysql_error());\n";
                         $ajax_lnk .= "     while(\$row = mysql_fetch_row(\$result))\n";
                         $ajax_lnk .= "     {\n";
                         $ajax_lnk .= "          if(\$ret == '')\n";
                         $ajax_lnk .= "          {\n";
                         $ajax_lnk .= "               \$ret .= sprintf(\"%s:%s\", \$row[0], \$row[1]);\n";
                         $ajax_lnk .= "          }\n";
                         $ajax_lnk .= "          else\n";
                         $ajax_lnk .= "          {\n";
                         $ajax_lnk .= "               \$ret .= sprintf(\";%s:%s\", \$row[0], \$row[1]);\n";
                         $ajax_lnk .= "          }\n";
                         $ajax_lnk .= "     }\n";
                         $ajax_lnk .= "     echo \$ret;\n";
                         $ajax_lnk .= "     exit;\n";
                         $ajax_lnk .= "}\n";
                    }
               }
          }
     }
     
     
     $ajax_lnk .= "if(isset(\$_REQUEST['oper']))\n";
     $ajax_lnk .= "{\n";
     $ajax_lnk .= "     if(\$_REQUEST['oper'] == 'del')\n";
     $ajax_lnk .= "     {\n";
     $ajax_lnk .= "          \$id = \$_REQUEST['id'];\n";
     $ajax_lnk .= "          \$sql = \"UPDATE $table SET excluido='1' WHERE id" . strtolower($table) . "='\$id'\";\n";
     $ajax_lnk .= "          \$result = mysql_query(\$sql, \$db);\n";
     $ajax_lnk .= "          if(\$result == 0)\n";
     $ajax_lnk .= "          {\n";
     $ajax_lnk .= "               printf(\"<h1>Falha: %s</h1>\", mysql_error(\$db));\n";
     $ajax_lnk .= "               printf(\"<h3>%s</h3>\", \$sql);\n";
     $ajax_lnk .= "               exit;\n";
     $ajax_lnk .= "          }\n";
     $ajax_lnk .= "     }\n";
     $ajax_lnk .= "     else if(\$_REQUEST['oper'] == 'add')\n";
     $ajax_lnk .= "     {\n";
     $SQLP1 = '';
     $SQLP2 = '';
     $UP = '';
     $var = '';
     $ajax_lnk .= get_var($db, $table, $page, $aba, $var, $SQLP1, $SQLP2, $UP);
     $ajax_lnk .= $var;
     $ajax_lnk .= $SQLP1 . $SQLP2;
     $ajax_lnk .= "          \$result = mysql_query(\$sql, \$db);\n";
     $ajax_lnk .= "          if(\$result == 0)\n";
     $ajax_lnk .= "          {\n";
     $ajax_lnk .= "               printf(\"<h1>Falha: %s</h1>\", mysql_error(\$db));\n";
     $ajax_lnk .= "               printf(\"<h3>%s</h3>\", \$sql);\n";
     $ajax_lnk .= "               exit;\n";
     $ajax_lnk .= "          }\n";
     $ajax_lnk .= "     }\n";
     $ajax_lnk .= "     else if(\$_REQUEST['oper'] == 'edit')\n";
     $ajax_lnk .= "     {\n";
     $ajax_lnk .= "          \$id = \$_REQUEST['id'];\n";
     $ajax_lnk .= $var;
     $ajax_lnk .= $UP;
     $ajax_lnk .= "          \$result = mysql_query(\$sql, \$db);\n";
     $ajax_lnk .= "          if(\$result == 0)\n";
     $ajax_lnk .= "          {\n";
     $ajax_lnk .= "               printf(\"<h1>Falha: %s</h1>\", mysql_error(\$db));\n";
     $ajax_lnk .= "               printf(\"<h3>%s</h3>\", \$sql);\n";
     $ajax_lnk .= "               exit;\n";
     $ajax_lnk .= "          }\n";
     $ajax_lnk .= "     }\n";
     $ajax_lnk .= "}\n";
     $ajax_lnk .= "if(isset(\$_REQUEST['rows']))\n";
     $ajax_lnk .= "{\n";
     $ajax_lnk .= "     \$qtype = 1;\n";
     $ajax_lnk .= "     \$where = \"WHERE excluido='0'\";\n";
     $ajax_lnk .= "     \$page = \$_REQUEST['page'];\n";
     $ajax_lnk .= "     \$limit = \$_REQUEST['rows'];\n";
     $ajax_lnk .= "     \$sidx = \$_REQUEST['sidx'];\n";
     $ajax_lnk .= "     \$sord = \$_REQUEST['sord'];\n";
     $ajax_lnk .= "     if(isset(\$_REQUEST['_search']))\n";
     $ajax_lnk .= "     {\n";
     $ajax_lnk .= "          if(\$_REQUEST['_search'] == 'true')\n";
     $ajax_lnk .= "          {\n";
     $ajax_lnk .= "               if(isset(\$_REQUEST['searchField']))\n";
     $ajax_lnk .= "               {\n";
     $ajax_lnk .= "                    \$where .= \" AND \" . \$_REQUEST['searchField'] . ' LIKE ' . \"'%\" . \$_REQUEST['searchString'] . \"%'\";\n";
     $ajax_lnk .= "               }\n";
     $ajax_lnk .= "          }\n";
     $ajax_lnk .= "     }\n";
     $ajax_lnk .= "     if(!\$sidx)\n";
     $ajax_lnk .= "     {\n";
     $ajax_lnk .= "          \$sidx = 1;\n";
     $ajax_lnk .= "     }\n";
     $ajax_lnk .= "     if(isset(\$_REQUEST['id".strtolower($page)."']))\n";
     $ajax_lnk .= "     {\n";
     $ajax_lnk .= "          \$where .= \" AND id".strtolower($page)."= '\" . \$_REQUEST['id".strtolower($page)."'] . \"'\";\n";
     $ajax_lnk .= "     }\n";
     $ajax_lnk .= "     \$SQL = \"SELECT COUNT(*) AS count FROM " . $table . " \$where\";\n";
     $ajax_lnk .= "     \$result = mysql_query(\$SQL);\n";
     $ajax_lnk .= "     \$row = mysql_fetch_array(\$result, MYSQL_ASSOC);\n";
     $ajax_lnk .= "     \$count = \$row['count'];\n";
     $ajax_lnk .= "     if(\$count > 0)\n";
     $ajax_lnk .= "     {\n";
     $ajax_lnk .= "          \$total_pages = ceil(\$count / \$limit);\n";
     $ajax_lnk .= "     }\n";
     $ajax_lnk .= "     else\n";
     $ajax_lnk .= "     {\n";
     $ajax_lnk .= "          \$total_pages = 0;\n";
     $ajax_lnk .= "     }\n";
     $ajax_lnk .= "     if(\$page > \$total_pages)\n";
     $ajax_lnk .= "     {\n";
     $ajax_lnk .= "          \$page = \$total_pages;\n";
     $ajax_lnk .= "     }\n";
     $ajax_lnk .= "     \$start = \$limit * \$page - \$limit;\n";
     $ajax_lnk .= "     if(\$start < 0)\n";
     $ajax_lnk .= "     {\n";
     $ajax_lnk .= "          \$start = 0;\n";
     $ajax_lnk .= "     }\n";
     $WHERE = '';
     $SQL = '';
     $ROW = '';
     $FROM = '';
     get_where($db, $table, $page ,$aba, $WHERE, $SQL, $ROW, $FROM);
     $ajax_lnk .= "     \$where .= \"" . $WHERE . "\";\n";
     $ajax_lnk .= "     " . $SQL . $FROM . " \$where ORDER BY \$sidx \$sord LIMIT \$start , \$limit\";\n";
     //$ajax_lnk .= "     \$SQL = \"SELECT NOME as nome,Tipo_Dependente.descricao as tipo,DATE_FORMAT(NASCIMENTO,'%d/%m/%Y') as nascimento,idfuncionario_dependente as id, idfuncionario, estado, Tipo_Sexo.descricao as sexo,rg,DATE_FORMAT(expedicao,'%d/%m/%Y') as expedicao,cpf, observacao FROM Funcionario_Dependente,Tipo_Dependente,Tipo_Sexo \$where ORDER BY \$sidx \$sord LIMIT \$start , \$limit\";\n";
     $ajax_lnk .= "     \$result = mysql_query(\$SQL) or die(\"Falha na execucao da query\" . mysql_error());\n";
     $ajax_lnk .= "     \$responce->page = \$page;\n";
     $ajax_lnk .= "     \$responce->total = \$total_pages;\n";
     $ajax_lnk .= "     \$responce->records = \$count;\n";
     $ajax_lnk .= "     \$i = 0;\n";
     $ajax_lnk .= "     while(\$row = mysql_fetch_array(\$result, MYSQL_ASSOC))\n";
     $ajax_lnk .= "     {\n";
     $ajax_lnk .= "          \$responce->rows[\$i]['id'] = \$row[id];\n";
     $ajax_lnk .= "          \$responce->rows[\$i]['cell'] = array(".$ROW.");\n";
     $ajax_lnk .= "          \$i++;\n";
     $ajax_lnk .= "     }\n";
     $ajax_lnk .= "     echo json_encode(\$responce);\n";
     $ajax_lnk .= "}\n";
     $ajax_lnk .= "?>\n";
     
     $FILE = "". strtolower($page) . "_" . strtolower($table) . ".ajax.php";
     $fPHP = fopen($FILE, "w");
     if(!$fPHP)
     {
          echo "<h1>Falha na Criacao do Arquivo '" . $FILE . "'</h1>";
          exit;
     }
     fprintf($fPHP, "%s", $ajax_lnk);
     fclose($fPHP);
}

?>
